package com.ayoka.ui.training;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.ayoka.R;
import com.ayoka.base.BaseActivity;
import com.ayoka.databinding.ActivityTrainingPrepareBinding;
import com.ayoka.mvp.TrainingDetailMVP.ITrainingDetailView;
import com.ayoka.mvp.TrainingDetailMVP.TrainingDetailModel.ResponseTrainingDetailBean;
import com.ayoka.mvp.TrainingDetailMVP.TrainingDetailModel.TrainingQuestionBean;
import com.ayoka.mvp.TrainingDetailMVP.TrainingDetailPresenter;
import com.jcp.baselibrary.model.ToolBarOptions;


/**
 * @author jcp
 * @ClassName: TopicListActivity
 * @Description:
 * @date 2018-12-28 下午4:21:18
 */
public class TrainingPrepareActivity extends BaseActivity<ActivityTrainingPrepareBinding, TrainingDetailPresenter> implements ITrainingDetailView {

    public static void start(Activity activity, long meditationId, String title) {
        Intent intent = new Intent(activity, TrainingPrepareActivity.class);
        intent.putExtra("MeditationId", meditationId);
        intent.putExtra("Title", title);
        activity.startActivity(intent);
    }

    private long meditationId;
    private String title;

    private ResponseTrainingDetailBean responseTrainingDetailBean;

    private int currentIndex;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            meditationId = intent.getLongExtra("MeditationId", 0);
            title = intent.getStringExtra("Title");
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_training_prepare;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(title);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        mPresenter.getTrainingDetailData(meditationId);
    }

    @Override
    protected TrainingDetailPresenter getPresenter() {
        return new TrainingDetailPresenter();
    }


    @Override
    public void configTrainingDetailData(ResponseTrainingDetailBean responseTrainingDetailBean) {
        if (responseTrainingDetailBean == null) return;
        this.responseTrainingDetailBean = responseTrainingDetailBean;

        showNextQuestionFragment();
    }

    @Override
    public void onQuitClick() {

    }

    @Override
    public void onOperationSuccess() {

    }

    @Override
    public void onBackPressed() {
        try {
            FragmentManager fragmentManager = getSupportFragmentManager();
            int count = fragmentManager.getBackStackEntryCount();
            if (count > 1) {
                fragmentManager.popBackStackImmediate();
                currentIndex--;
            } else {
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
            finish();
        }
    }

    public void showNextQuestionFragment() {
        if (currentIndex < responseTrainingDetailBean.meditation.Questions.size()) {
            TrainingQuestionBean questionBean = responseTrainingDetailBean.meditation.Questions.get(currentIndex);
            TrainingPrepareFragment questionFragment = TrainingPrepareFragment.newInstance(questionBean);
            try {
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.setCustomAnimations(R.anim.slide_right_in,
                        R.anim.slide_left_out,
                        R.anim.slide_left_in,
                        R.anim.slide_right_out);
                transaction.replace(R.id.frameLayout, questionFragment)
                        .addToBackStack(null)
                        .commitAllowingStateLoss();
            } catch (Exception e) {
                e.printStackTrace();
            }
            currentIndex++;
        } else {
            TrainingDetailActivity.start(this, meditationId);
            finish();
        }
    }
}
